package com.six666.medicaldevicetestingsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.six666.medicaldevicetestingsystem.dto.Observe_DTO;
import com.six666.medicaldevicetestingsystem.entity.Pair;
import com.six666.medicaldevicetestingsystem.entity.UserAccount;
import com.six666.medicaldevicetestingsystem.mapper.UserAccount_Mapper;
import com.six666.medicaldevicetestingsystem.service.Account_Service;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class Account_Service_Impl extends ServiceImpl<UserAccount_Mapper, UserAccount> implements Account_Service {
    @Override
    public Pair<Integer, UserAccount> login(String username, String password) {
        QueryWrapper<UserAccount> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("UserName",username);
        queryWrapper.eq("PassWord",password);
        if (list(queryWrapper).isEmpty()){
            QueryWrapper<UserAccount> queryWrapper_name=new QueryWrapper<>();
            queryWrapper_name.eq("UserName",username);
            if (list(queryWrapper_name).isEmpty()){
                return new Pair<>(2,null);
            }
            return new Pair<>(3,null);
        }
        return new Pair<>(1,list(queryWrapper).get(0));
    }

    @Override
    public Pair<Integer, UserAccount> sign(UserAccount user) {
        QueryWrapper<UserAccount> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("UserName",user.getUsername());
        if (!list(queryWrapper).isEmpty()){
            return new Pair<>(2,null);
        }
        boolean res=save(user);
        if(!res){
            return new Pair<>(3,null);
        }
        return new Pair<>(1,getOne(queryWrapper));
    }

    @Override
    public Observe_DTO observe(int user_id){
        QueryWrapper<UserAccount> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("AccountID",user_id);
        UserAccount user=getOne(queryWrapper);
        Observe_DTO observe=new Observe_DTO();
        observe.setUser_id(user_id);
        observe.setName(user.getUsername());
        observe.setContact(user.getContact());
    return observe;
    }
    @Override
    public List<UserAccount> get_by_id_list(List<Integer> ongoingTestAccountIdList) {
        List<UserAccount> res=new ArrayList<>();
        for (Integer integer : ongoingTestAccountIdList) {
            QueryWrapper<UserAccount> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("AccountId", integer);
            res.add(getOne(queryWrapper));
        }
        return res;
    }

    @Override
    public UserAccount get_by_id(int id) {
        QueryWrapper<UserAccount> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("AccountId",id);
        return getOne(queryWrapper);
    }

}
